/**
 * ============================================================
 * ======= 多音字 ===
 */
// 数据源
export function polyphonicGetLibraryWords(words){
	if(!words) {
		return null
	}
	const wordsObj = JSON.parse(words)
	const wordsArr = [];
	const pingyi = [];
	for(let key in wordsObj) {
		const splitKey = key.split("|")
		const split = wordsObj[key].split("|")
		wordsArr.push(split[0])
		// 拼音
		const py = {}
		py.rowId = splitKey[0]
		py.id = splitKey[1]
		py.words = split[0]
		py.pingyi = split[1].split("-")
		pingyi.push(py)
	}
	// result
	return {
		words: wordsArr,
		pingyi
	}
}
// 渲染
export function polyphonicRenderWords(words){
	if(!words || words && !words.length) {
		return []
	}
	const arr = []
	words.forEach(item => {
		const formatWords = polyphonicGetLibraryWords(item)?.pingyi;
		const obj = {}
		formatWords.forEach(w => {
			obj[`${w.rowId}|${w.wordsId}`] = `${w.words}|${w.words}`
		})
		arr.push(JSON.stringify(obj))
	})
	return arr
}
// 阅读解析拼音
export function polyphonicPingYiWords(words){
	//  {text: "端端正正|歪歪扭扭", rowId: "694", id: "1"}
	if(!words || words && !words.length) {
		return []
	}
	const result = []
	words.forEach(item => {
		const formatWords = polyphonicGetLibraryWords(item)?.pingyi;
		
		formatWords.forEach(w => {
			const text = w.pingyi.map(t => w.words)
			const obj = {
				...w,
				text: text.join("|")
			}
			result.push(obj)
		})
	})
	return result
}
// 获取组词
export function getGroupWords(words, basis){
	let value = words
	if(words.indexOf('|') !== -1) {
		value = words.split('|')[0]
	}
	const wordsArray = basis.split(' ')
	const result = wordsArray.filter(word => word.includes(value))
	return result
}